package com.lykj.lib_base.internal.utils

import android.app.Activity
import android.os.Environment
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.FileIOUtils
import com.blankj.utilcode.util.LogUtils
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import java.io.File


/**
 * ================================================
 * 设备工具类,读写外置SDK中设备码(同一)
 * ================================================
 *
 * @author gosha
 * @date 2022/9/2-15:14
 */
object DeviceUtil {
    //读取唯一设备码
    open fun getUniqueDeviceId(activity: Activity): String {
        var deviceId = ""
        XXPermissions.with(activity)
            .permission(Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                    //读取
                    try {
                        if (Environment.getExternalStorageState()
                                .equals(Environment.MEDIA_MOUNTED)
                        ) {
                            LogUtils.e("外置存储正常挂载")
                        } else {
                            return
                        }
                        val rootPath =
                            Environment.getExternalStorageDirectory()
                        val configPath = "$rootPath/config"
                        val fileDirPath = File(configPath)
                        val file = File("$fileDirPath/lykj.ini")

                        if (!fileDirPath.exists()) {
                            fileDirPath.mkdirs()
                        }
                        if (!file.exists()) {
                            saveUniqueDeviceId(activity)
                        }
                        deviceId = FileIOUtils.readFile2String(file)
                        if (deviceId.isNullOrBlank()) {
                            saveUniqueDeviceId(activity)
                        }

                        LogUtils.e("Path: $file Read: ${deviceId}")
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }

                override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
                    ToastUtils.showShort(activity, "请先授予读写存储权限")
                }
            })

        return deviceId
    }

    //保存设备码
    open fun saveUniqueDeviceId(activity: Activity) {
        val deviceId = DeviceUtils.getUniqueDeviceId()
        XXPermissions.with(activity)
            .permission(Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                    //读取
                    try {
                        val rootPath = Environment.getExternalStorageDirectory()
                        val configPath = "$rootPath/config"
                        val fileDirPath = File(configPath)
                        val filePath = "$fileDirPath/lykj.ini"
                        if (!fileDirPath.exists()) {
                            fileDirPath.mkdirs()
                        }
                        //检查权限
                        FileIOUtils.writeFileFromString(filePath, deviceId)

                        LogUtils.e("Path: $filePath Write: ${deviceId}")
                    } catch (ex: Exception) {
                        ex.printStackTrace()
                    }
                }

                override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
                    ToastUtils.showShort(activity!!.applicationContext, "请先授予读写存储权限")
                }
            })
    }
}